<?php
/**
*  Create On 2014-12-25
*  Author yiwei
*  QQ:1006629314
**/
class class_student extends Model{
	private $table = 'xdf_class_student';
	public function getStudentNameByPeriod($class_id){
		$sql = 'SELECT name FROM `xdf_user` AS U WHERE U.id IN
				(SELECT student_id FROM `'.$this->table.'` WHERE class_id = '.$class_id.')';
		return $this->fetchAll($sql);
	}
	
	public function getStudentIdByPeriod($period_id){
		$sql = 'SELECT c.student_id FROM `xdf_period` p LEFT JOIN `xdf_class_student` AS c ON p.class_id = c.class_id WHERE p.id = '.$period_id;
		return $this->fetchAll($sql);
	}
	
	public function getStudentInfoByClass($class_id){
		$sql = 'SELECT P.type,P.clientid,P.devicetoken,U.name FROM `xdf_user` AS U
				LEFT JOIN `xdf_plant` AS P ON U.id = P.user_id
				WHERE U.id IN
				(SELECT id FROM `'.$this->table.'` WHERE class_id = '.$class_id.')';
		return $this->fetchAll($sql);
	}
	
	public function getClassInfoByStudent($stu_id){
		$sql = 'SELECT * FROM `xdf_class` AS C
				LEFT JOIN `xdf_class_student` AS CS ON C.id = CS.class_id
				WHERE CS.student_id ='.$stu_id;
		return $this->fetchAll($sql);
	}
	
	public function getTeacherInfoByStudent($stu_id){
		$sql = 'SELECT DISTINCT(U.id),U.name FROM `xdf_user` AS U 
				LEFT JOIN `xdf_period` AS P ON U.id = P.teacher_id
				LEFT JOIN `xdf_class_student` AS CS ON CS.class_id = P.class_id
				WHERE CS.student_id = '.$stu_id;
		return $this->fetchAll($sql);
	}
	
	public function getClassIdByCode($classCode){
		$sql = 'SELECT id FROM `xdf_class` WHERE code = "'.$classCode.'"';
		return $this->fetchRow($sql);
	}
}